package com.tzbank.back.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tzbank.back.service.RoleService;
import com.tzbank.entity.backstage.Role;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class RoleController {

	@Autowired
	private RoleService roleService;

	/**
	 * 添加角色
	 * @param request
	 * @param role
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "insertRole")
	public Map<String, Object> insertRole(HttpServletRequest request, @RequestBody Role role) {
		try {
			return this.roleService.insertRole(role);
		} catch (Exception e) {
			log.error("RoleController insertRole error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 修改角色
	 * @param request
	 * @param role
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "updateRole")
	public Map<String, Object> updateRole(HttpServletRequest request, @RequestBody Role role) {
		try {
			return this.roleService.updateRole(role);
		} catch (Exception e) {
			log.error("RoleController updateRole error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 删除角色
	 * @param request
	 * @param role
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "deleteRole")
	public Map<String, Object> deleteRole(HttpServletRequest request, @RequestBody Role role) {
		try {
			return this.roleService.deleteRole(role);
		} catch (Exception e) {
			log.error("RoleController deleteRole error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 通过自增主键查询角色
	 * @param request
	 * @param autoId
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@GetMapping(value = "selectRoleByAutoId")
	public Map<String, Object> selectRoleByAutoId(HttpServletRequest request,
			@RequestParam(required = true) Integer autoId) {
		try {
			return this.roleService.selectRoleByAutoId(autoId);
		} catch (Exception e) {
			log.error("RoleController selectRoleByAutoId error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 查询角色
	 * @param request
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@GetMapping(value = "selectRoleList")
	public Map<String, Object> selectRoleList(HttpServletRequest request) {
		try {
			return this.roleService.selectRoleList();
		} catch (Exception e) {
			log.error("RoleController selectRoleList error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

}
